package com.example.dao;

import java.util.List;
import java.util.Map;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.example.domain.StudentClass;

@Repository
public interface StudentClassDao extends JpaRepository<StudentClass, Integer> {

    @Transactional
    @Modifying
    Integer deleteByStudentClassId(Integer studentClassId);

    List<StudentClass> findByStudentClassId(Integer studentClassId);

    @Transactional
    @Modifying
    Integer deleteByStudentClassIdIn(List<Integer> listId);

    @Query(nativeQuery = true, value = "SELECT u.user_Id as userId,u.account as userName,u.nickName \r\n" +
            "from (users u left join user_role ur on u.user_Id = ur.user_Id )\r\n" +
            "left join role r on r.role_Id = ur.role_Id\r\n" +
            "where r.roleName = '学生' and  u.user_Id not in (select sc.user_Id from student_class sc)")
    List<Map<String, Object>> findAllStudents();

    @Query(nativeQuery = true, value = "SELECT distinct c.class_Id as classId,ct.typeName,c.className,t.termName\r\n" +
            "FROM ((classes c INNER JOIN student_class sc ON c.class_Id = sc.class_Id)\r\n" +
            "INNER JOIN class_type ct ON ct.class_type_Id = c.class_type_Id) \r\n" +
            "INNER JOIN term t ON t.term_Id = c.term_Id")
    List<Map<String, Object>> findAllClass();


    @Query(nativeQuery = true, value = "SELECT c.className,u.account as userName,u.nickName,sc.* \r\n" +
            "FROM (users u INNER JOIN student_class sc ON u.user_Id = sc.user_Id)\r\n" +
            "INNER JOIN classes c ON c.class_Id = sc.class_Id")
    List<Map<String, Object>> queryAllStudentAndClassInfo();

    @Query(nativeQuery = true, value = "SELECT c.className,u.account as userName,u.nickName,sc.* \r\n" +
            "FROM (users u INNER JOIN student_class sc ON u.user_Id = sc.user_Id)\r\n" +
            "INNER JOIN classes c ON c.class_Id = sc.class_Id\r\n" +
            "WHERE c.className like CONCAT('%',:className,'%')")
    List<Map<String, Object>> findByClassNameLike(@Param("className") String className);

    @Query(nativeQuery = true, value = "SELECT c.className,u.account as userName,u.nickName,sc.* \r\n" +
            "FROM (users u INNER JOIN student_class sc ON u.user_Id = sc.user_Id)\r\n" +
            "INNER JOIN classes c ON c.class_Id = sc.class_Id\r\n" +
            "WHERE u.account like CONCAT('%',:userName,'%')")
    List<Map<String, Object>> findByUserNameLike(@Param("userName") String userName);

    @Query(nativeQuery = true, value = "SELECT c.className,u.account as userName,u.nickName,sc.* \r\n" +
            "FROM (users u INNER JOIN student_class sc ON u.user_Id = sc.user_Id)\r\n" +
            "INNER JOIN classes c ON c.class_Id = sc.class_Id\r\n" +
            "WHERE u.nickName like CONCAT('%',:nickName,'%')")
    List<Map<String, Object>> findByNickNameLike(@Param("nickName") String nickName);

    @Query(nativeQuery = true, value = "SELECT distinct c.class_Id as classId,ct.typeName,c.className,t.termName\r\n" +
            "FROM (classes c INNER JOIN class_type ct ON ct.class_type_Id = c.class_type_Id) \r\n" +
            "INNER JOIN term t ON t.term_Id = c.term_Id")
    List<Map<String, Object>> queryAllClassInfo();

}
